package com.jasic.view;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.jasic.R;
import com.jasic.util.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Jasic
 * Date: 12-11-26
 */
public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;

    private String TAG = GlobalVariable.TAG;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("------->创建CameraActivity，进行相机预览....");
        setContentView(R.layout.preview);

        /**
         * 创建相机实例
         */
        mCamera = CameraUtil.getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);

        Camera.PictureCallback pictureCallback = new JasicPictureCallback();

        /**
         * 摄像按钮
         */
        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                mCamera.takePicture(new JasicShutterCallback(),null,new JasicPictureCallback());
            }
        });
    }

    /**
     * 1、为了获得一个图像，使用Camera.takePicture()方法，这个方法有三个参数来接收从camera获得的数据
     * 2、In order to receive data in a JPEG format, you must implement an Camera.PictureCallback interface to receive the image data and write it to a file
     */
    private class JasicPictureCallback implements Camera.PictureCallback {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            File picFile = FileUtil.getOutputMediaFile(GlobalConstant.MEDIA_TYPE_IMAGE);

            if (picFile == null) {
                Log.d(TAG, "Error creating media file, check storage permissions: ");
                return;
            }

            try {
                FileOutputStream fos = new FileOutputStream(picFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }

    }

    /**
     * 摄像那一刻的回调函数
     */
    private class JasicShutterCallback implements Camera.ShutterCallback{
        @Override
        public void onShutter() {
            Toast.makeText(
                    CameraActivity.this,"擦～～～",Toast.LENGTH_SHORT
            ).show();
        }
    }
}
